package com.yq.skinlib.sizechange;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.yq.skinlib.R;
import com.yq.skinlib.widgets.TintTextView;


/**
 * 创建者     Maggie
 * 创建时间   2019/3/20 2:36 PM
 * 描述	     可动态修改字号大小的textview
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${}
 */
public class ChangeSizeTextview extends TintTextView implements SizeChangeable {
    private float mTextSize = 0;

    public ChangeSizeTextview(Context context) {
        super(context);
    }

    public ChangeSizeTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTextSize = getTextSize();
        changeTextSize();
    }

    public ChangeSizeTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextSize = getTextSize();
        changeTextSize();
    }

    @Override
    public void changeTextSize() {
        if (mTextSize != 0) {
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize * ChangeTextSizeManager.getInstance().getTextSize());
        }
    }

    @Override
    public void setTextAppearance(int resId) {
        super.setTextAppearance(resId);
        TypedArray appearance = getContext().obtainStyledAttributes(resId, R.styleable.TintTextAppearance);
        if (appearance.hasValue(R.styleable.TintTextAppearance_android_textSize)) {
            mTextSize = appearance.getDimensionPixelSize(R.styleable.TintTextAppearance_android_textSize,0);
            changeTextSize();
        }

    }

    @Override
    public void setTextAppearance(Context context, int resId) {
        super.setTextAppearance(context, resId);
        TypedArray appearance = getContext().obtainStyledAttributes(resId, R.styleable.TintTextAppearance);
        if (appearance.hasValue(R.styleable.TintTextAppearance_android_textSize)) {
            mTextSize = appearance.getDimensionPixelSize(R.styleable.TintTextAppearance_android_textSize,0);
            changeTextSize();
        }
    }

    @Override
    public void setTextSize(float textSize) {
        super.setTextSize(mTextSize * ChangeTextSizeManager.getInstance().getTextSize());
        mTextSize = textSize;
    }
}
